function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

$(function () {

    $(".input_sub").click(function (e) {
        e.preventDefault()

        var signature = $("#signature").val()
        var username = $("#username").val()
        var gender = $(".gender:checked").val()
        // var gender = $('input[name="gender"][checked]').val();
        if (!username) {
            alert('请输入昵称')
            return
        }
        if (!gender) {
            alert('请选择性别')
        }

        var params = {
            "signature": signature,
            "username": username,
            "gender": gender,
            "csrfmiddlewaretoken": $("*[name='csrfmiddlewaretoken']").val(),
        }

        $.ajax({
            url: "/base_info/",
            type: "post",
            contentType: "application/json",
            headers: {
                "X-CSRFToken": getCookie("csrftoken")
            },
            data: JSON.stringify(params),
            success: function (resp) {
                if (resp.errno == "0") {
                    // 更新父窗口里的用户名
                    $('.user_center_name', parent.document).html(params['username'])
                    $('#username', parent.document).html(params['username'])
                    $('.input_sub').blur()
                    alert(resp.errmsg)
                } else {
                    alert(resp.errmsg)
                }
            }
        })
    })
})